package com.b510.service.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.b510.bean.Person;
import com.b510.service.PersonService;

/**
 * 业务bean
 * 
 * @author Hongten
 * 
 */
public class PersonServiceBean implements PersonService {

    /**
     * 数据源
*/
    private DataSource dataSource;
    /**
     * spring提供的jdbc操作辅助类
*/
    private JdbcTemplate jdbcTemplate;

    // 设置数据源
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public void save(Person person) {
        int rs = jdbcTemplate.update("insert into bajieList(vod_id,vod_cid,vod_name,vod_title,vod_actor,vod_pic,vod_filmtime,vod_reurl,vod_content,vod_length,score,videoCount,imageSize,vod_year,vod_director,vod_area,vod_language,vod_type,filmlevel)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                new Object[] { person.getVod_id(),person.getVod_cid(), person.getVod_name(),person.getVod_title(),person.getVod_actor(),person.getVod_pic(),person.getFilmtime(),person.getVod_reurl(),person.getVod_content(),person.getVod_length(),person.getScore(),person.getVideoCount(),person.getImageSize(),person.getVod_year(),person.getVod_director(),person.getVod_area(),person.getVod_language(),person.getVod_type(),person.getFilmlevel() }, 
                new int[] { java.sql.Types.BIGINT,java.sql.Types.BIGINT , java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.DATE, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.FLOAT,java.sql.Types.INTEGER,java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});
    }

    public void update(Person person) {
        /*jdbcTemplate.update("update person set media=? where id=?",
                new Object[] { person.getMedia(), person.getId()}, new int[] {
                        java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
*/
    }

    public Person getPerson(String link) {
        try {
			Person person = (Person) jdbcTemplate.queryForObject(
			        "select * from person where link=?", new Object[] { link },
			        new int[] { java.sql.Types.VARCHAR }, new PersonRowMapper());
			return person;
		} catch (Exception e) {
			System.out.println("personServiceBean:"+e.getMessage());
			return null;
		}
    }

    @SuppressWarnings("unchecked")
    public List<Person> getPerson() {
        List<Person> list = jdbcTemplate.query("select * from person where name is not null limit 100", new PersonRowMapper());
        return list;

    }

    public void delete(Integer id) {
        jdbcTemplate.update("delete from person where id = ?", new Object[] { id },
                new int[] { java.sql.Types.INTEGER });

    }
}